# @Time:        2024/7/7 上午10:48
# @USER:        lihuizhuo
# @Site:        www.swust.edu.cn
# @File:        M
# @Software:    PyCharm
# @license:     Copyright(C), swust
# @Author:      huizhuo
# @Email:       huizhuoli@foxmail.com
# @Version:     V1.1.0


import os
import sys
import pygame
from pytmx import TiledObjectGroup

parent_dir = os.path.dirname(
    os.path.dirname(os.path.abspath(__file__)))
print(parent_dir)
sys.path.insert(0, parent_dir)

# sys.path.insert(0, "/Users/lihuizhuo/PycharmProjects/2024-edu-prj/demo2")

from actor.soldier import ShootSoldier, WalkSoldier
from scene.tiled_render import TiledRenderer

def game_run():
    """
    游戏运行
    :return:
    """
    # pygame setup
    pygame.init()
    # set up the display 窗口
    screen = pygame.display.set_mode((1280, 720))
    clock = pygame.time.Clock()
    running = True

    tmx_map = TiledRenderer("resources/tmx/tunnel_map.tmx")
    group = pygame.sprite.Group()
    obstacle_group = pygame.sprite.Group()


    for layer in tmx_map.tmx_data.visible_layers:
        if isinstance(layer, TiledObjectGroup):
            if layer.name == '小铁':
                for obj in layer:
                    if obj.name == '小铁':
                        work_soldier = WalkSoldier("resources/images/boy1.png",
                                                   obj.x, obj.y,
                                                   obj.name, 100)

            if layer.name == "日本人":
                for obj in layer:

                    shoot_soldier = ShootSoldier("resources/images/chinese/red_army/red_army1.png",
                                                     "resources/images/chinese/red_army/red_army1-1.png",
                                                     obj.x , obj.y , "战士", 100)

                    group.add(shoot_soldier)
            if layer.name == "障碍物":
                for obj in layer:
                    obstacle = pygame.sprite.Sprite()
                    obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                    obstacle_group.add(obstacle)






    while running:
        # poll for events
        # pygame.QUIT event means the user clicked X to close your window
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                work_soldier.run(event.key)


        # fill the screen with a color to wipe away anything from last frame
        # screen.fill("purple")
        #
        tmx_map.render_map(screen)
        work_soldier.draw(surface=screen)


        for sprite in group:
            sprite.draw(screen)

        # RENDER YOUR GAME HERE

        # flip() the display to put your work on screen
        pygame.display.flip()

        clock.tick(20)  # limits FPS to 60

    pygame.quit()


if __name__ == '__main__':
    # print(ShootDir.not_shoot)
    game_run()
